<template>
  <div class="login-container">
    <div class="login-card">
      <div class="login-header">
        <h1 class="login-title">登录</h1>
        <p class="login-subtitle">欢迎回来，请登录您的账户</p>
      </div>

      <form @submit.prevent="handleLogin" class="login-form">
        <div class="form-group">
          <label for="email" class="form-label">邮箱地址</label>
          <input
            id="email"
            v-model="email"
            type="email"
            class="form-input"
            :class="{ 'error': emailError }"
            placeholder="请输入您的邮箱地址"
            required
          />
          <div v-if="emailError" class="error-message">{{ emailError }}</div>
        </div>

        <div class="form-group">
          <label for="password" class="form-label">密码</label>
          <input
            id="password"
            v-model="password"
            type="password"
            class="form-input"
            :class="{ 'error': passwordError }"
            placeholder="请输入您的密码"
            required
          />
          <div v-if="passwordError" class="error-message">{{ passwordError }}</div>
        </div>

        <div class="form-options">
          <label class="checkbox-label">
            <input v-model="rememberMe" type="checkbox" class="checkbox">
            <span class="checkbox-text">记住我</span>
          </label>
          <router-link to="/auth/forgot-password" class="forgot-link">
            忘记密码？
          </router-link>
        </div>

        <button
          type="submit"
          class="login-btn"
          :disabled="isLoading || !isFormValid"
          :class="{ 'loading': isLoading }"
        >
          <span v-if="!isLoading">登录</span>
          <span v-else>登录中...</span>
        </button>

        <div v-if="loginError" class="error-message login-error">
          {{ loginError }}
        </div>
      </form>

      <div class="login-footer">
        <p class="register-link">
          还没有账户？
          <router-link to="/auth/register" class="link">立即注册</router-link>
        </p>
      </div>

      <div class="divider">
        <span class="divider-text">或</span>
      </div>

      <div class="social-login">
        <button class="social-btn google-btn" @click="handleGoogleLogin">
          <svg class="social-icon" viewBox="0 0 24 24">
            <path fill="#4285F4" d="M22.56 12.25c0-.78-.07-1.53-.2-2.25H12v4.26h5.92c-.26 1.37-1.04 2.53-2.21 3.31v2.77h3.57c2.08-1.92 3.28-4.74 3.28-8.09z"/>
            <path fill="#34A853" d="M12 23c2.97 0 5.46-.98 7.28-2.66l-3.57-2.77c-.98.66-2.23 1.06-3.71 1.06-2.86 0-5.29-1.93-6.16-4.53H2.18v2.84C3.99 20.53 7.7 23 12 23z"/>
            <path fill="#FBBC05" d="M5.84 14.09c-.22-.66-.35-1.36-.35-2.09s.13-1.43.35-2.09V7.07H2.18C1.43 8.55 1 10.22 1 12s.43 3.45 1.18 4.93l2.85-2.22.81-.62z"/>
            <path fill="#EA4335" d="M12 5.38c1.62 0 3.06.56 4.21 1.64l3.15-3.15C17.45 2.09 14.97 1 12 1 7.7 1 3.99 3.47 2.18 7.07l3.66 2.84c.87-2.6 3.3-4.53 6.16-4.53z"/>
          </svg>
          使用 Google 登录
        </button>
      </div>
    </div>
  </div>
</template>

<script>
import { useAuthStore } from '../../store/auth'
import { useRouter } from 'vue-router'

export default {
  name: 'Login',
  setup() {
    const authStore = useAuthStore()
    const router = useRouter()
    return { authStore, router }
  },
  data() {
    return {
      email: '',
      password: '',
      rememberMe: false,
      isLoading: false,
      emailError: '',
      passwordError: '',
      loginError: ''
    }
  },
  computed: {
    isFormValid() {
      return this.email && this.password && !this.emailError && !this.passwordError
    }
  },
  methods: {
    async handleLogin() {
      if (!this.isFormValid) return

      this.isLoading = true
      this.loginError = ''
      this.emailError = ''
      this.passwordError = ''

      try {
        const userData = {
          email: this.email,
          password: this.password
        }

        await this.authStore.loginUser(userData)
        
        // 登录成功，显示成功消息并重定向
        this.$toast?.success('登录成功！')
        this.router.push('/')
      } catch (error) {
        console.error('登录失败:', error)
        
        if (error.response?.data) {
          const errorData = error.response.data
          
          // 处理字段特定错误
          if (errorData.email) {
            this.emailError = Array.isArray(errorData.email) ? errorData.email[0] : errorData.email
          }
          if (errorData.password) {
            this.passwordError = Array.isArray(errorData.password) ? errorData.password[0] : errorData.password
          }
          
          // 处理通用错误
          if (errorData.non_field_errors) {
            this.loginError = Array.isArray(errorData.non_field_errors) 
              ? errorData.non_field_errors[0] 
              : errorData.non_field_errors
          } else if (errorData.detail) {
            this.loginError = errorData.detail
          } else if (!this.emailError && !this.passwordError) {
            this.loginError = '登录失败，请检查您的邮箱和密码'
          }
        } else {
          this.loginError = '网络错误，请稍后重试'
        }
      } finally {
        this.isLoading = false
      }
    },

    handleGoogleLogin() {
      // TODO: 实现Google登录
      console.log('Google登录功能待实现')
      this.$toast?.info('Google登录功能即将推出')
    }
  }
}
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 20px;
}

.login-card {
  background: white;
  border-radius: 16px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
  padding: 40px;
  width: 100%;
  max-width: 400px;
}

.login-header {
  text-align: center;
  margin-bottom: 32px;
}

.login-title {
  font-size: 28px;
  font-weight: 700;
  color: #1a202c;
  margin: 0 0 8px 0;
}

.login-subtitle {
  font-size: 16px;
  color: #718096;
  margin: 0;
}

.login-form {
  margin-bottom: 24px;
}

.form-group {
  margin-bottom: 20px;
}

.form-label {
  display: block;
  font-size: 14px;
  font-weight: 600;
  color: #374151;
  margin-bottom: 6px;
}

.form-input {
  width: 100%;
  padding: 12px 16px;
  border: 2px solid #e5e7eb;
  border-radius: 8px;
  font-size: 16px;
  transition: all 0.2s ease;
  box-sizing: border-box;
  background: white;
  color: #1f2937;
  font-weight: 500;
}

.form-input:focus {
  outline: none;
  border-color: #667eea;
  box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
  color: #111827;
}

.form-input.error {
  border-color: #ef4444;
  box-shadow: 0 0 0 3px rgba(239, 68, 68, 0.1);
  color: #1f2937;
}

.form-input::placeholder {
  color: #6b7280;
  font-weight: 400;
}

.form-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
}

.checkbox-label {
  display: flex;
  align-items: center;
  cursor: pointer;
}

.checkbox {
  margin-right: 8px;
}

.checkbox-text {
  font-size: 14px;
  color: #374151;
}

.forgot-link {
  font-size: 14px;
  color: #667eea;
  text-decoration: none;
}

.forgot-link:hover {
  text-decoration: underline;
}

.login-btn {
  width: 100%;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border: none;
  border-radius: 8px;
  padding: 14px 24px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s ease;
  position: relative;
}

.login-btn:hover:not(:disabled) {
  transform: translateY(-1px);
  box-shadow: 0 8px 25px rgba(102, 126, 234, 0.3);
}

.login-btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
  transform: none;
}

.login-btn.loading {
  color: transparent;
}

.login-btn.loading::after {
  content: '';
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 20px;
  height: 20px;
  border: 2px solid transparent;
  border-top: 2px solid white;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  0% { transform: translate(-50%, -50%) rotate(0deg); }
  100% { transform: translate(-50%, -50%) rotate(360deg); }
}

.error-message {
  font-size: 14px;
  color: #ef4444;
  margin-top: 6px;
}

.login-error {
  text-align: center;
  margin-top: 16px;
  padding: 12px;
  background-color: #fef2f2;
  border: 1px solid #fecaca;
  border-radius: 6px;
}

.login-footer {
  text-align: center;
  margin-bottom: 24px;
}

.register-link {
  font-size: 14px;
  color: #718096;
  margin: 0;
}

.link {
  color: #667eea;
  text-decoration: none;
  font-weight: 600;
}

.link:hover {
  text-decoration: underline;
}

.divider {
  position: relative;
  text-align: center;
  margin: 24px 0;
}

.divider::before {
  content: '';
  position: absolute;
  top: 50%;
  left: 0;
  right: 0;
  height: 1px;
  background: #e5e7eb;
}

.divider-text {
  background: white;
  color: #9ca3af;
  padding: 0 16px;
  font-size: 14px;
}

.social-login {
  margin-top: 24px;
}

.social-btn {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 12px 16px;
  border: 2px solid #e5e7eb;
  border-radius: 8px;
  background: white;
  color: #374151;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s ease;
}

.social-btn:hover {
  border-color: #d1d5db;
  background: #f9fafb;
}

.social-icon {
  width: 20px;
  height: 20px;
  margin-right: 12px;
}

.google-btn {
  margin-bottom: 12px;
}

@media (max-width: 480px) {
  .login-card {
    padding: 24px;
    margin: 10px;
  }
  
  .login-title {
    font-size: 24px;
  }
}
</style>